<?php

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

$mmsi = empty($_GET['mmsi'])?0:intval($_GET['mmsi']);
$view = empty($_GET['view'])?'myship':trim($_GET['view']);

if(intval($_GET['pid']) || !empty($_SGLOBAL['supe_uid'])){

	$pid = empty($_GET['pid'])?'0':intval($_GET['pid']);

	$trends_a = array('FAR_BERTH'=>'离泊','NEAR_BERTH'=>'靠泊','MOVE_BERTH'=>'移泊');
	$status_a = array('AUDITED'=>'已审核','ASSIGNED'=>'已排班','FINISHED'=>'已完成','NO_CONFIRMED'=>'申报','CONFIRMED'=>'确报','CANCEL'=>'取消','REJECTED'=>'驳回','HIS'=>'历史记录','BALANCE'=>'结算','REJECT_HIS'=>'驳回历史记录');
	$planlist = $planids = array();

	if($pid == '0'){
		//我所在公司的调度计划用户名
		$query = $_SGLOBAL['db']->query("SELECT username FROM ".tname('planuser')." WHERE comid = ".$space['com_id']." LIMIT 1");
		if($value = $_SGLOBAL['db']->fetch_array($query)) {
			$planusername = $value['username'];
			$attlist = array();

			//我关注的船舶
			$query = $_SGLOBAL['db']->query("SELECT ship_name FROM ".tname('ship_att')." WHERE uid='".$_SGLOBAL['supe_uid']."' ORDER BY id desc");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$attlist[] = $value['ship_name'];
			}

			//我关注的船舶的调度计划
			foreach($attlist as $v){
				$query = $_SGLOBAL['db']->query("SELECT id, shipcallsign, plantime, shipname, shipename,nationality, trends, origin, stop, status, pilot, pilot2, agency, dispatcher
				FROM ".tname('plan')." WHERE shipename='".$v."' ORDER BY plantime desc LIMIT 1");
				if($value = $_SGLOBAL['db']->fetch_array($query)) {
					$value['trends'] = $trends_a[$value['trends']];
					$value['status'] = $status_a[$value['status']];
					$planlist[] = $value;
					$planids[] = $value['id'];
				}
			}
			$pidstr = "&planids=".implode('_',$planids);

		}
	}else if(!empty($pid) && $pid != '0'){
		/*查看分享者的好友ID*/
		$query = $_SGLOBAL['db']->query("SELECT uid FROM ".tname('shareplan')." WHERE id= ".$pid);
		if($v = $_SGLOBAL['db']->fetch_array($query)){
			$query = $_SGLOBAL['db']->query("SELECT fuid FROM ".tname('friend')." WHERE uid= ".$v['uid']);
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$fuidarr[] = $value['fuid']; //好友ID
			}
			array_push($fuidarr,$v['uid']);
		}
		if(in_array($_SGLOBAL['supe_uid'],$fuidarr)){
			$query = $_SGLOBAL['db']->query("SELECT planid FROM ".tname('shareplan')." WHERE id= ".$pid);
			if($value = $_SGLOBAL['db']->fetch_array($query)) {
				$planids = explode(',',$value['planid']); //分享的调度计划的id
				foreach($planids as $id){
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('plan')." WHERE id='".$id."' LIMIT 1");
					if($result = $_SGLOBAL['db']->fetch_array($query)) {
						$result['trends'] = $trends_a[$result['trends']];
						$result['status'] = $status_a[$result['status']];
						$planlist[] = $result; //好友分享的计划
					}
				}
			}
		}
		/*查看分享者的同事ID*/
		$query = $_SGLOBAL['db']->query("SELECT uid FROM ".tname('shareplan')." WHERE id= ".$pid);
		if($v = $_SGLOBAL['db']->fetch_array($query)){
			$query = $_SGLOBAL['db']->query("SELECT tagid FROM ".tname('tagspace')." WHERE uid = ".$v['uid']);
			if($v1 = $_SGLOBAL['db']->fetch_array($query)) {
				$query = $_SGLOBAL['db']->query("SELECT uid FROM ".tname('tagspace')." WHERE tagid = ".$v1['tagid']." and uid != ".$v['uid']);
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$cuidarr[] = $value['uid']; //同事ID
				}
			}
			//print_r($cuidarr);
		}
		if(in_array($_SGLOBAL['supe_uid'],$cuidarr)){
			$query = $_SGLOBAL['db']->query("SELECT planid FROM ".tname('shareplan')." WHERE id= ".$pid);
			if($value = $_SGLOBAL['db']->fetch_array($query)) {
				$planids = explode(',',$value['planid']); //分享的调度计划的id
				foreach($planids as $id){
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('plan')." WHERE id='".$id."' LIMIT 1");
					if($result = $_SGLOBAL['db']->fetch_array($query)) {
						$result['trends'] = $trends_a[$result['trends']];
						$result['status'] = $status_a[$result['status']];
						$planlist[] = $result; //同事分享的计划
					}
				}
			}
		}
	}


	//分页
	/*$pperpage = 10;
	$pperpage = mob_perpage($pperpage);

	$pcount = 0;
	$ppage = empty($_GET['ppage'])?0:intval($_GET['ppage']);
	if($ppage<1) $ppage = 1;
	$pstart = ($ppage-1)*$pperpage;*/

	//引航计划表查询
	//$puid = empty($_GET['id'])?$_SGLOBAL['supe_uid']:intval($_GET['id']);

	//处理查询
	//$thepurl = "space.php?do=findship_page&pid=".$pid."&ajaxdiv=shipplan&inajax=1";

	/*查找计划表中所有的用户*/
	/*$query = $_SGLOBAL['db']->query("SELECT username FROM ".tname('shareplan'));
	while($value = $_SGLOBAL['db']->fetch_array($query)) {
		$namearr[] = $value['username'];
	}
	//我的好友ID
	$query = $_SGLOBAL['db']->query("SELECT fuid FROM ".tname('friend')." WHERE uid= ".$_SGLOBAL['supe_uid']);
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$fuidarr[] = $value['fuid']; //我的好友ID
	}*/



	/*查找我的计划*/
	/*if($puid == $_SGLOBAL['supe_uid']){
		//我的计划
		$query = $_SGLOBAL['db']->query("SELECT shipcallsign, plantime, shipname, shipename,nationality, trends, origin, stop, status, pilot, pilot2, agency, dispatcher
		FROM ".tname('plan')." WHERE userid='".$_SGLOBAL['supe_username']."' ORDER BY plantime desc
		LIMIT $pstart,$pperpage");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$value['trends'] = $trends_a[$value['trends']];
			$value['status'] = $status_a[$value['status']];
			$planlist[] = $value;
			$pcount++;
		}
		if($pstart === 0 && $pcount>=$pperpage){
			$pmulti = "&lt;  1~10  <a href=\"javascript:;\" onclick=\"ajaxget('".$thepurl."&start=".($pstart+$pperpage)."&ajaxdiv=shipplan', 'shipplan')\">&gt;</a> ";
		}
	}else if(in_array($puid,$fuidarr)){
		/*查看好友分享的计划*/
		/*$query = $_SGLOBAL['db']->query("SELECT username FROM ".tname('share')." WHERE uid= ".$puid." and type = 'plan' and friend <=1");
		if($value = $_SGLOBAL['db']->fetch_array($query)) {
			$fname = $value['username']; //分享计划的好友名称
		}
		if($fname){
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('plan')." WHERE userid='".$fname."' ORDER BY plantime desc LIMIT $pstart,$pperpage");
			while($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['trends'] = $trends_a[$value['trends']];
				$value['status'] = $status_a[$value['status']];
				$planlist[] = $value; //好友分享的计划
			}
		}
	}else if(in_array($puid,$cuidarr)){
		/*查找同事的计划*/
		/*if(!empty($space['com_id'])){
			$query = $_SGLOBAL['db']->query("SELECT username FROM ".tname('shareplan')." where comid = ".$space['com_id']);
			while($value = $_SGLOBAL['db']->fetch_array($query)) {
				if($value['username']){
					$queryplan = $_SGLOBAL['db']->query("SELECT shipcallsign, plantime, shipname, shipename,nationality, trends, origin, stop, status, pilot, pilot2, agency, dispatcher FROM ".tname('plan')." WHERE userid='".$value['username']."' ORDER BY plantime desc LIMIT $pstart,$pperpage");
				}
			}
			//关注船舶列表
			$list = array();
			$query = $_SGLOBAL['db']->query("SELECT ship_name FROM ".tname('ship_att')." WHERE uid='$_SGLOBAL[supe_uid]' ORDER BY id DESC");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$list = $value['ship_name'];
			}
			while ($value = $_SGLOBAL['db']->fetch_array($queryplan)) {
				if(in_array($value['shipename'],$list)){
					$value['trends'] = $trends_a[$value['trends']];
					$value['status'] = $status_a[$value['status']];
					$planlist[] = $value;
					$pcount++;
				}else{
					continue;
				}
			}
			if($pstart === 0 && $pcount>=$pperpage){
				$pmulti = "&lt;  1~10  <a href=\"javascript:;\" onclick=\"ajaxget('".$thepurl."&start=".($pstart+$pperpage)."&ajaxdiv=shipplan', 'shipplan')\">&gt;</a> ";
			}
		}
	}*/
}


//查询
if($view == 'search') {
	//搜索条件过滤
	if($searchkey = stripsearchkey($_POST['searchkey'])) {
		if(intval($searchkey)>0){
			echo "<script>";
			echo "parent.search_ship_bymmsi('$searchkey');";
			echo "</script><ajaxok></ajaxok>";
		}elseif(strpos($searchkey, ' ')>0){
			$searchkey = strtoupper($searchkey);
			echo "<script>";
			echo "parent.search_ship_byname('$searchkey');";
			echo "</script><ajaxok></ajaxok>";
		}else{
			$searchkey = strtoupper($searchkey);
			echo "<script>";
			echo "parent.search_ship_bycallsign('$searchkey');";
			echo "</script><ajaxok></ajaxok>";
		}
		exit();
	}

}elseif($view == 'getcommont'){

	if($mmsi){
		$commont_list = getCommontList($mmsi);
	}

}elseif($view == 'getatt'){

	if($mmsi){
		$query = $_SGLOBAL['db']->query("SELECT Id FROM ".tname('ship_att')." WHERE ship_mmsi='$mmsi' AND uid='$_SGLOBAL[supe_uid]'");
		$result = '';
		$value = $_SGLOBAL['db']->fetch_array($query);
		if($value){
			$result = 'kly';
		}else{
			$result = 'khn';
		}
	}

}elseif(intval($_GET['getlist']) == 1){

	$uid = empty($_GET['uid'])?0:intval($_GET['uid']);
	if($uid > 0 && $_GET['kly']=='klykhn'){
		$ship_list = array();
		$query = $_SGLOBAL['db']->query("SELECT ship_mmsi FROM ".tname('ship_att')." WHERE uid ='$uid' ORDER BY dateline DESC");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$ship_list[] = $value['ship_mmsi'];
		}
		$shiplist = implode(',',$ship_list);
	}
	echo $shiplist;exit();

}elseif($view == 'getfriendships'){

	$fuid = empty($_GET['fuid'])?0:intval($_GET['fuid']);

	if($fuid > 0){
		$ship_list = array();
		$query = $_SGLOBAL['db']->query("SELECT ship_mmsi,ship_name FROM ".tname('ship_att')." WHERE uid ='$fuid' ORDER BY dateline DESC");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			//$ship_list[] = $value['ship_name'].":".$value['ship_mmsi'];
			$ship_list[] = $value;
		}
		//$shiplist = implode(',',$ship_list);
	}

}elseif($view == 'getattlist'){
	
	$list = array();
	$query = $_SGLOBAL['db']->query("SELECT ship_mmsi,ship_name FROM ".tname('ship_att')." WHERE uid='$_SGLOBAL[supe_uid]' ORDER BY id DESC LIMIT 0,10");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$list[] = $value;
	}

}elseif($view == "getmap"){

	//搜索条件过滤
	if($searchkey = stripsearchkey($_POST['searchkey'])) {
		include_once(S_ROOT.'./data/data_gangkou.php');
		if(array_key_exists($searchkey,$_SGLOBAL['gangkou'])){
			echo "<script>";
			echo "parent.local_map(".$_SGLOBAL['gangkou'][$searchkey].",11);";
			echo "</script>";
		}
	}
	exit();

}else{

	//分页
	$perpage = 5;
	$perpage = mob_perpage($perpage);

	$friendlist = $fuids = $mtaglist = array();
	$count = 0;
	$page = empty($_GET['page'])?0:intval($_GET['page']);
	if($page<1) $page = 1;
	$start = ($page-1)*$perpage;

	//处理查询
	$theurl = "space.php?do=findship_page&view=getflist&ajaxdiv=friend_list&inajax=1";

	//好友的船舶
	if($space['friendnum']) {
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM (SELECT ship.uid from ".tname('friend')." main
					RIGHT JOIN ".tname('ship_att')." ship ON ship.uid=main.fuid
					WHERE main.uid='$space[uid]' AND main.status='1' GROUP BY ship.uid ) tlist"), 0);
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT s.uid, s.username, s.name, s.namestatus, main.num, ship.Id as shipid
				FROM ".tname('friend')." main
				LEFT JOIN ".tname('space')." s ON s.uid=main.fuid
				RIGHT JOIN ".tname('ship_att')." ship ON ship.uid=main.fuid
				WHERE main.uid='$space[uid]' AND main.status='1' GROUP BY s.username
				ORDER BY main.num DESC, main.dateline DESC
				LIMIT $start,$perpage");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				realname_set($value['uid'], $value['username'], $value['name'], $value['namestatus']);
				$friendlist[$value['uid']] = $value;
			}
		}

		if($count > $perpage){
			$realpages = @ceil($count / $perpage);
			$str = '<strong>1</strong>';
			for($i = 1; $i<$realpages;$i++){
				$str .= "<a href='javascript:;' onclick='fship_page(".($i+1).")'>".($i+1)."</a>";
			}
			$str .= " <a href='javascript:;' onclick='fship_page(2)'>>></a>";
		}

	}

	//关注船舶列表
	$list = array();
	$query = $_SGLOBAL['db']->query("SELECT ship_mmsi,ship_name FROM ".tname('ship_att')." WHERE uid='$_SGLOBAL[supe_uid]' ORDER BY id DESC");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$list[] = $value;
	}

	//同事的船舶
	if($space['com_id']) {
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM (SELECT ship.uid from ".tname('tagspace')." main
					RIGHT JOIN ".tname('ship_att')." ship ON ship.uid=main.uid
					WHERE main.tagid='$space[com_id]' AND main.uid != '$_SGLOBAL[supe_uid]' GROUP BY ship.uid ) tlist"), 0);
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT s.uid, s.username, s.name, s.namestatus, ship.Id as shipid
				FROM ".tname('tagspace')." main
				LEFT JOIN ".tname('space')." s ON s.uid=main.uid
				RIGHT JOIN ".tname('ship_att')." ship ON ship.uid=main.uid
				WHERE main.tagid='$space[com_id]' AND main.uid != '$_SGLOBAL[supe_uid]'
				GROUP BY s.uid ORDER BY s.uid DESC LIMIT 0,$perpage");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				realname_set($value['uid'], $value['username'], $value['name'], $value['namestatus']);
				$mtaglist[$value['uid']] = $value;
			}
		}

		if($count > $perpage){
			$realpages = @ceil($count / $perpage);
			$mtagstr = '<strong>1</strong>';
			for($i = 1; $i<$realpages;$i++){
				$mtagstr .= "<a href='javascript:;' onclick='fship_mtagpage(".($i+1).")'>".($i+1)."</a>";
			}
			$mtagstr .= " <a href='javascript:;' onclick='fship_mtagpage(2)'>>></a>";
		}
	}
}

realname_get();
include_once template("space_findship");

function getCommontList($mmsi){
	global $_SGLOBAL;
	$commont_list = array();
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('shipcomment')." WHERE mmsi='$mmsi' ORDER BY dateline DESC");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		if(ckfriend($value['uid'], $value['friend'], '')) {
			realname_set($value['uid'], $value['username']);
			$commont_list[] = $value;
		}
	}
	return $commont_list;
}

?>